<template>
  <div>
    <el-form :model="form" ref="ruleForm" :rules="rules">
      <el-form-item label="账号" :label-width="formLabelWidth" prop="username">
        <el-input
          v-model="form.username"
          autocomplete="off"
          :disabled="model.is_edit ? true : false"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth" prop="nickname">
        <el-input
          v-model="form.nickname"
          autcomplete="off"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
        <el-switch
          v-model="form.status"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item
        v-if="!model.is_edit"
        prop="password"
        :label-width="formLabelWidth"
        label="密码"
      >
        <el-input type="password" v-model="form.password" clearable></el-input>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth">
        <el-button type="primary" @click="onSubmit"
          >{{ model.is_edit ? "立即更新" : "立即创建" }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { fetchDetail, store, save } from "@/api/admin";
export default {
  data() {
    let usernameRule = [
      { min: 5, max: 20, message: "长度在 5 到 20 个字符", trigger: "blur" }
    ];
    let passwordRule = [];
    if (!this.model.is_edit) {
      usernameRule.push({
        required: true,
        message: "请输入账号",
        trigger: "blur"
      });
      passwordRule = [
        {
          required: true,
          message: "请输入密码",
          trigger: "blur"
        },
        { min: 5, max: 20, message: "长度在 5 到 20 个字符", trigger: "blur" }
      ];
    }
    return {
      formLabelWidth: "120px",
      form: {
        username: "",
        nickname: "",
        status: 0,
        password: ""
      },
      rules: {
        username: usernameRule,
        nickname: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur"
          },
          { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" }
        ],
        password: passwordRule
      }
    };
  },
  props: {
    model: {
      default: {}
    }
  },
  created() {
    if (this.model.is_edit) {
      let id = this.model.id;
      fetchDetail(id).then(res => {
        if (res.data.code === "200") {
          let data = res.data.data;
          this.form = {
            id: data.id,
            username: data.username,
            nickname: data.nickname,
            status: data.status
          };
        }
      });
    }
  },
  methods: {
    onSubmit() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          if (this.model.is_edit) {
            save(this.form).then(res => {
              if (res.data.code === "200") {
                this.$message({
                  message: "更新成功",
                  type: "success"
                });
                this.$emit("success", this.form);
              } else {
                this.$message({
                  message: res.data.msg,
                  type: "error"
                });
                this.$emit("error", this.form);
              }
            });
          } else {
            store(this.form).then(res => {
              if (res.data.code === "200") {
                this.$message({
                  message: "添加成功",
                  type: "success"
                });
                this.$emit("success", this.form);
              } else {
                this.$message({
                  message: res.data.msg,
                  type: "error"
                });
                this.$emit("error", this.form);
              }
            });
          }
        }
      });
    }
  }
};
</script>

<style scoped></style>
